﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChatDBAccess
{
    public class Create
    {
        public static void CreateUser(string name, string pass, string email)
        {
            ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
            var res =
                (from u in contex.Users
                where u.UserName == name
                select u).FirstOrDefault();
            if (res == null)
            {
                User user = new User()
                {
                    UserName = name,
                    PassWord = pass,
                    Email = email,
                    RegDate = DateTime.Now
                };

                contex.Users.InsertOnSubmit(user);

                contex.SubmitChanges();
            }
            else
            {
                throw new ArgumentException("Invalid UserName");
            }
        }

        public static void CreateChannel(string name, int ownerId)
        {
            ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
            var res =
                (from c in contex.Channels
                where c.Name == name
                select c).FirstOrDefault();
            if (res == null)
            {
                Channel chann = new Channel();
                chann.Name = name;
                chann.OwnerId = ownerId;

                contex.Channels.InsertOnSubmit(chann);

                contex.SubmitChanges();
            }
            else
            {
                throw new ArgumentException("Invalid Channel Name");
            }
        }

        public static void CreateUTUMessage(int senderId, int receiverId, string text)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            UTUMessage message = new UTUMessage()
            {
                SenderId = senderId,
                ReceiverId = receiverId,
                Text = text,
                PostDate = DateTime.Now
            };

            context.UTUMessages.InsertOnSubmit(message);

            context.SubmitChanges();
        }

        public static void CreateUTCMessage(int senderId, int channelId, string text)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            UTCMessage message = new UTCMessage()
            {
                SenderId = senderId,
                ChannelId = channelId,
                Text = text,
                PostDate = DateTime.Now
            };

            context.UTCMessages.InsertOnSubmit(message);

            context.SubmitChanges();
        }

    }
}
